package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCategoryMapper;
import com.qf.pojo.DtsCategory;
import com.qf.pojo.DtsRole;
import com.qf.vo.CategorySellAmts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class DtsCategoryServiceImpl implements DtsCategoryService{

    @Autowired
    private DtsCategoryMapper dtsCategoryMapper;
    @Override
    public List<CategorySellAmts> categorySell() {
        return dtsCategoryMapper.categorySell();
    }

    @Override
    public IPage<DtsCategory> selectPage(int page, int limit, String sort, String order, Integer id, String name) {
        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!StringUtils.isEmpty(name),"name",name);
        queryWrapper.eq(!StringUtils.isEmpty(id),"id",id);
        queryWrapper.eq("deleted",0);
        queryWrapper.orderBy(!StringUtils.isEmpty(sort),"asc".equalsIgnoreCase(order),sort);

        return dtsCategoryMapper.selectPage(new Page<DtsCategory>(page,limit),queryWrapper);
    }
   //一级商品类目
    @Override
    public List<Map<String, Object>> options() {
        QueryWrapper<DtsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id value ,name label");//启用
        return dtsCategoryMapper.selectMaps(queryWrapper);
    }
    //添加类目
    @Override
    public void create(DtsCategory dtsCategory) {
        dtsCategory.setAddTime(new Date());
        dtsCategory.setUpdateTime(new Date());

        dtsCategoryMapper.insert(dtsCategory);

    }
    //更新类目
    @Override
    public void update(DtsCategory dtsCategory) {
        dtsCategory.setUpdateTime(new Date());
        dtsCategoryMapper.updateById(dtsCategory);
    }
    //删除类目
    @Override
    public void delete(DtsCategory dtsCategory) {
        dtsCategory.setDeleted(true);
        dtsCategoryMapper.updateById(dtsCategory);
    }
}
